#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
//**********************************客户端结构体******服务器接收和发送的结构体
typedef struct
{
    int sock_fd;
    struct sockaddr_in my_addr;
} TcpSocket;
//**********************************服务端结构体
typedef struct
{
    int sock_fd;
    struct sockaddr_in my_addr;
} TcpServer;
//**********************************客户端连接服务器
int TcpSockConnect(TcpSocket *client,char* SERVER_IP,unsigned short Port)
{
    client->my_addr.sin_family = AF_INET;
    client->my_addr.sin_port = htons(Port);
    client->my_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    bzero(&(client -> my_addr.sin_zero),8);
    if((client->sock_fd = socket(PF_INET,SOCK_STREAM, 0)) == -1)
    {
        perror("client create sock_fd failed\n");
        return -1;
    }
    if (connect(client->sock_fd, (struct sockaddr *)&(client->my_addr), sizeof(client->my_addr)) < 0)
    {
        perror("connect server failed\n");
        return -1;
    }
    return 0;
}
//*****************************发送函数
int TcpSockSend(TcpSocket *client,char *buff,int size)
{
    int len = send(client->sock_fd, buff, size, 0);
	return len;
}
//**********************************接收函数
int TcpSockRecv(TcpSocket *client,char *buff,int size)
{
    int len = recv(client->sock_fd, buff, size,0);
	return len;
}
//**********************************关闭TcpSocket的文件描述符
void TcpSockClose(TcpSocket *client)
{
    close(client->sock_fd);
}
//**********************************初始化服务端
int initTcpServer(TcpServer *server,char* IP,unsigned short ServerPort,int QUEUE)
{
    server -> my_addr.sin_family = AF_INET;
    server -> my_addr.sin_port = htons(ServerPort);
    server -> my_addr.sin_addr.s_addr = htonl(inet_addr(IP));
    bzero(&(server -> my_addr.sin_zero),8);
    if((server->sock_fd = socket(PF_INET,SOCK_STREAM, 0)) == -1)
    {
        perror("server create sock_fd error\n");
        return -1;
    }
	//unsigned int value = 0x1;
	//setsockopt(server->sock_fd,SOL_SOCKET,SO_REUSEADDR,(void *)&value,sizeof(value)); 
    if(bind(server->sock_fd,(struct sockaddr *)&(server->my_addr),sizeof(server->my_addr))==-1)
    {
        perror("server bind error\n");
        return -1;
    }
    if(listen(server->sock_fd,QUEUE) == -1)
    {
        perror("server listen error\n");
        return -1;
    }
    return 0;
}
//**********************************服务端accept获取一个连接
int TcpServAccept(TcpServer *server,TcpSocket *cli)
{
    struct sockaddr_in client_addr;
    socklen_t len = sizeof(client_addr);
    int new_fd = accept(server->sock_fd, (struct sockaddr*)&client_addr, &len);
    if(new_fd < 0)
    {
        perror("server accept error\n");
        return -1;
    }
    cli->sock_fd = new_fd;
    cli->my_addr = client_addr;
    return 0;
}
//**********************************销毁TcpServer
void TcpServClose(TcpServer *server)
{
    close(server->sock_fd);
}
